/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.paint.views.components;

import com.nokia.example.paint.helpers.ImageLoader;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * Toolbar that contains all the painting
 * tools.
 */
public class Toolbar {

    private int width;
    private Vector tools;
    private Image buffer;
    private Image background;
    private Graphics bg;
    private int buf_x = 0;
    private int buf_y = 0;
    public static int HEIGHT = 49;
    private int last_x = 0;

    public Toolbar(int screenwidth) {
        width = screenwidth;
        HEIGHT = (width > 240) ? 74 : 49;
        tools = new Vector();
        addBasicTools();
        buffer = Image.createImage(width, HEIGHT);
        if (HEIGHT == 74) {
            background = ImageLoader.getInstance().loadImage("/toolbar_large.png");
        }
        else {
            background = ImageLoader.getInstance().loadImage("/toolbar_small.png");
        }
        bg = buffer.getGraphics();
    }

    private void addBasicTools() {
        final String keyboardType = System.getProperty(
                "com.nokia.keyboard.type");        
        if (!keyboardType.equals("OnekeyBack")) {
            tools.addElement(new Exit());
        }
        
        tools.addElement(new BrushSizeTool());
        tools.addElement(new SaveTool());
        tools.addElement(new ClearTool());
        setToolsPositions();
    }

    private void setToolsPositions() {
        int l = tools.size();
        int w = width / tools.size();
        //starting positions
        int xpos = 0;
        int ypos = 2;
        for (int i = l - 1; i >= 0; i--) {
            Button tool = (Button) tools.elementAt(i);            
            int x = xpos + w / 2 - tool.getWidth() / 2;
            xpos += w;
            tool.setPosition(x, ypos);
        }
    }

    public void render(Graphics g) {
        bg.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
        int l = tools.size();
        for (int i = 0; i < l; i++) {
            Button tool = (Button) tools.elementAt(i);
            tool.render(bg);
        }
        g.drawImage(buffer, buf_x, buf_y, Graphics.TOP | Graphics.LEFT);
    }

    public void addTool(Button tool, int index) {
        tools.insertElementAt(tool, index);
        setToolsPositions();
    }

    public void pointerPressed(int x, int y) {
        int l = tools.size();
        for (int i = 0; i < l; i++) {
            Button tool = (Button) tools.elementAt(i);
            tool.pressed(x - buf_x, y);
        }
    }

    public void pointerUnpressed(int x, int y) {
        int l = tools.size();
        for (int i = 0; i < l; i++) {
            Button tool = (Button) tools.elementAt(i);
            tool.unpressed(x - buf_x, y);
        }
    }

    public void dragToolBar(int x, int y) {
        if (last_x == 0) {
            last_x = x;
        }
        int diff = x - last_x;
        if (diff > 0) {
            buf_x += 4;
            last_x = 0;
        }
        else if (diff < 0) {
            buf_x -= 4;
            last_x = 0;
        }
    }
}
